package com.mcx.modules.common;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class MMO_DataStream {
    public DataInputStream inputStream;
    public DataOutputStream outputStream;

    public byte ReadByte() throws IOException {
        return inputStream.readByte();
    }

    public void WriteByte(byte value) throws IOException {
        outputStream.writeByte(value);
    }

    /**
     * 从流中读取一个short数据
     * @return
     * @throws IOException
     */
    public short ReadShort() throws IOException {
        byte[] arr = new byte[2];
        inputStream.read(arr, 0, 2);
        return BitConverter.ToInt16(arr, 0);
    }

    /**
     * 把一个short数据写入流
     * @param value
     * @throws IOException
     */
    public void WriteShort(short value) throws IOException {
        byte[] arr = BitConverter.GetBytes(value);
        outputStream.write(arr, 0, arr.length);
    }

    /**
     * 从流中读取一个int数据
     * @return
     * @throws IOException
     */
    public int ReadInt() throws IOException {
        byte[] arr = new byte[4];
        inputStream.read(arr, 0, 4);
        return BitConverter.ToInt32(arr, 0);
    }

    /**
     * 把一个int数据写入流
     * @param value
     * @throws IOException
     */
    public void WriteInt(int value) throws IOException {
        byte[] arr = BitConverter.GetBytes(value);
        outputStream.write(arr, 0, arr.length);
    }

    /**
     * 从流中读取一个long数据
     * @return
     * @throws IOException
     */
    public long ReadLong() throws IOException {
        byte[] arr = new byte[8];
        inputStream.read(arr, 0, 8);
        return BitConverter.ToInt64(arr, 0);
    }

    /**
     * 把一个long数据写入流
     * @param value
     * @throws IOException
     */
    public void WriteLong(long value) throws IOException {
        byte[] arr = BitConverter.GetBytes(value);
        outputStream.write(arr, 0, arr.length);
    }

    /**
     * 从流中读取一个float数据
     * @return
     * @throws IOException
     */
    public float ReadFloat() throws IOException {
        byte[] arr = new byte[4];
        inputStream.read(arr, 0, 4);
        return BitConverter.ToSingle(arr, 0);
    }

    /**
     * 把一个float数据写入流
     * @param value
     * @throws IOException
     */
    public void WriteFloat(float value) throws IOException {
        byte[] arr = BitConverter.GetBytes(value);
        outputStream.write(arr, 0, arr.length);
    }

    /**
     * 从流中读取一个double数据
     * @return
     * @throws IOException
     */
    public double ReadDouble() throws IOException {
        byte[] arr = new byte[8];
        inputStream.read(arr, 0, 8);
        return BitConverter.ToDouble(arr, 0);
    }

    /**
     * 把一个double数据写入流
     * @param value
     * @throws IOException
     */
    public void WriteDouble(double value) throws IOException {
        byte[] arr = BitConverter.GetBytes(value);
        outputStream.write(arr, 0, arr.length);
    }


    /**
     * 从流中读取一个bool数据
     * @return
     * @throws IOException
     */
    public Boolean ReadBool() throws IOException {
        return inputStream.readByte() == 1;
    }


    /**
     * 把一个bool数据写入流
     * @param value
     * @throws IOException
     */
    public void WriteBool(Boolean value) throws IOException {
        outputStream.writeByte((value ? 1 : 0));
    }

    public String ReadUTF8String() throws IOException {
        short len = ReadShort();
        byte[] arr = new byte[len];
        inputStream.read(arr, 0, len);
        return new String(arr, "UTF8");
    }

    /// <summary>
    /// 把一个string数据写入流
    /// </summary>
    /// <param name="str"></param>
    public void WriteUTF8String(String str) throws IOException {
        byte[] arr = str.getBytes(StandardCharsets.UTF_8);
        if (arr.length > 65535)
        {
            throw new RuntimeException("字符串超出范围");
        }
        WriteShort((short)arr.length);
        outputStream.write(arr, 0, arr.length);
    }
}
